package com.crazypos.common;

import lombok.Data;

@Data
public class ApiRestResponse<T> {
    private Integer status;
    private String msg;
    private T data;

    private static final int OK_CODE = 200;
    private static final String OK_MSG = "success";
    private static final int ERROR_CODE = -1;
    private static final String ERROR_MSG = "error";

    private ApiRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private ApiRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
        this.data = null;
    }

    private ApiRestResponse() {
        this(OK_CODE, OK_MSG);
    }

    public static ApiRestResponse success() {
        return new ApiRestResponse();
    }

    public static <T> ApiRestResponse success(T result) {
        ApiRestResponse response = new ApiRestResponse();
        response.setData(result);
        return response;
    }
    public static <T> ApiRestResponse success(int status,T result) {
        ApiRestResponse response = new ApiRestResponse();
        response.setData(result);
        response.setStatus(status);
        return response;
    }

    public static ApiRestResponse error() {
        return new ApiRestResponse(ERROR_CODE, ERROR_MSG);
    }

    public static ApiRestResponse error(Integer status, String errorMsg) {
        ApiRestResponse response = new ApiRestResponse();
        response.setMsg(errorMsg);
        response.setStatus(status);
        return response;
    }

}
